package arithmetic.socket.netty.chat;


import java.util.Scanner;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * netty 聊天室 客户端
 * @author jiangfeng
 * Created on 2020-02-04
 */
public class ChatClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup worker = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(worker).channel(NioSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.WARN))
                    .handler(new ChatClientInitializer());
            // 客户端链接:connect
            ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
            //channelFuture.channel().closeFuture().sync();
            Channel channel = channelFuture.channel();
            Scanner scanner= new Scanner(System.in);
            while (scanner.hasNextLine()){
                String msg = scanner.nextLine();
                channel.writeAndFlush(msg);
            }

        }finally {
            worker.shutdownGracefully();
        }
    }

   /* public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        while (scanner.hasNextLine()){
            System.out.println(scanner.nextLine()+"\r\n");
        }
    }*/
}
